package com.lzb.basic.imgedit;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

import java.io.InputStream;

/**
 * Fast bitmap drawable. Does not support states. it only
 * support alpha and colormatrix
 *
 * @author alessandro
 */
public class FastBitmapDrawable extends Drawable implements IBitmapDrawable {

    protected Bitmap mBitmap;
    protected Paint mPaint;
    protected float mRadius = 0;

    public FastBitmapDrawable(Bitmap b) {
        this(b, 0);
    }

    public FastBitmapDrawable(Bitmap b, float radius) {
        mBitmap = b;
        mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setFilterBitmap(true);
        mPaint.setAntiAlias(true);
        mRadius = radius;
    }

    public FastBitmapDrawable(Resources res, InputStream is) {
        this(BitmapFactory.decodeStream(is));
    }

    @Override
    public void draw(Canvas canvas) {
        //canvas.drawBitmap( mBitmap, 0.0f, 0.0f, mPaint );
        if (mBitmap != null) {
            canvas.save();

            RectF rectF = new RectF();
            rectF.left = 0;
            rectF.top = 0;
            rectF.right = rectF.left + mBitmap.getWidth();
            rectF.bottom = rectF.top + mBitmap.getHeight();

            Path path = new Path();
            path.addRoundRect(rectF, mRadius, mRadius, Path.Direction.CW);
            canvas.clipPath(path);
            canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);

            canvas.restore();
        }
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        mPaint.setColorFilter(cf);
    }

    @Override
    public int getIntrinsicWidth() {
        return mBitmap.getWidth();
    }

    @Override
    public int getIntrinsicHeight() {
        return mBitmap.getHeight();
    }

    @Override
    public int getMinimumWidth() {
        return mBitmap.getWidth();
    }

    @Override
    public int getMinimumHeight() {
        return mBitmap.getHeight();
    }

    public void setAntiAlias(boolean value) {
        mPaint.setAntiAlias(value);
        invalidateSelf();
    }

    public void setRadius(float radius) {
        if (mRadius != radius) {
            mRadius = radius;
            invalidateSelf();
        }
    }

    @Override
    public Bitmap getBitmap() {
        return mBitmap;
    }
}